﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using BrnMall.Core;
using BrnMall.Services;
using QIDong.WeApp.Models;
using WeiTap.Restful;

namespace QIDong.WeApp.Controllers
{
    /// <summary>
    /// 商城后台新闻控制器类
    /// </summary>
    public partial class NewsController : WebApi
    {
        /// <summary>
        /// 新闻类型列表
        /// </summary>
        [Obsolete]
        public List<NewsTypeInfo> NewsTypeList()
        {
            var model = News.GetNewsTypeList();
            return model;
        }

        /// <summary>
        /// 获取首页的内容类型和内容，内容也必须设置为首页展示
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="hasNews">是否包含新闻</param>
        /// <param name="ishome">是否首页</param>
        /// <returns></returns>
        public List<NewsTypeOutDto> StoreNewsTypeList(int storeId,int hasNews=0,int ishome=1)
        {

            var model = News.GetStoreNewsTypeList(storeId);
            var result=new List<NewsTypeOutDto>();
            foreach (var newsTypeInfo in model)
            {
                if(ishome==1&&!newsTypeInfo.IsHome)continue;
                if(ishome==0&&newsTypeInfo.IsHome)continue;
                var t = new NewsTypeOutDto {NewsTypeInfo = newsTypeInfo};
                if (hasNews == 1)
                {
                    var condition = News.GetNewsListCondition(t.NewsTypeInfo.NewsTypeId, string.Empty);
                    condition += " AND ishome=1";
                    if (t.NewsTypeInfo.DisplayType.Equals("pic_1"))
                    {
                        t.News = News.GetNewsList(1, 1, condition, storeId);
                    }
                    else if (t.NewsTypeInfo.DisplayType.Equals("pic_3"))
                    {
                        t.News = News.GetNewsList(3, 1, condition, storeId);
                    }
                    else
                    {
                        t.News = News.GetNewsList(10, 1, condition, storeId);
                    }
                }
                
                result.Add(t);
            }
            return result;
        }
       
        /// <summary>
        /// 新闻列表
        /// </summary>
        public NewsListModel NewsList(int storeid,string newsTitle, int newsTypeId = 0, int pageSize = 15, int pageNumber = 1)
        {
            try
            {
                string condition = News.GetNewsListCondition(newsTypeId, newsTitle);

                PageModel pageModel = new PageModel(pageSize, pageNumber, News.GetNewsCount(condition, storeid));

                var newsTypeList = News.GetStoreNewsTypeList(storeid);
                

                NewsListModel model = new NewsListModel()
                {
                    NewsList = News.GetNewsList(pageModel.PageSize, pageModel.PageNumber, condition, storeid),
                    PageModel = pageModel,
                    NewsTypeId = newsTypeId,
                    NewsTypeList = newsTypeList,
                    NewsTitle = newsTitle
                };

                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
           
        }
        /// <summary>
        /// 详细信息
        /// </summary>
        public NewsModel Details(int newsid)
        {
            try
            {
                //新闻id
                int newsId = newsid;
                if (newsId == 0)
                    throw new Exception("缺少内容参数");

                NewsInfo newsInfo = News.GetNewsById(newsId);
                if (newsInfo == null)
                    throw new Exception("找不到您访问的内容");

                NewsModel model = new NewsModel();
                model.NewsInfo = newsInfo;
                model.NewsTypeList = News.GetNewsTypeList();

                return model;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }


    }
}
